<template>
	<!--pages/delivery-address/delivery-address.wxml-->
	<view class="address-list">
		<view class="main">
			<view class="empty" v-if="addressList.length==0">
				<!-- <view class="img">
        <image src="http://jiales.gz-yami.com/addr.png"></image>
      </view> -->
				<view class="txt">您还没有收货地址</view>
			</view>
			<radio-group class="radio-group">
				<block v-for="(item, index) in addressList" :key="index">
					<uni-swipe-action>
						<uni-swipe-action-item :right-options="swipe_options" @click="delAddress($event,index)"
							@change="swipeChange($event, index)">

							<view class="address">
								<view class="personal" @tap="selAddrToOrder(item)">
									<view class="info-tit">
										<text class="name">{{item.recipientName}}</text>
										<text class="tel">{{item.recipientPhone}}</text>
										<image src="/static/images/icon/revise.png"
											@tap.stop="toEditAddress(item.userAddressId)"></image>
									</view>
									<view class="addr">

										<text class="addr-get">
											{{item.province}}{{item.city}}{{item.county}}{{item.detailAddress}}</text>
									</view>
								</view>
								<view class="select-btn">
									<view class="box">
										<label @tap="onDefaultAddr" :data-addrid="item.userAddressId">
											<radio :value="item.prodId" :checked="item.commonAddr==1" color="#eb2444">
											</radio>
											设为默认地址
										</label>
									</view>
								</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>

				</block>
			</radio-group>
		</view>
		<view class="footer" @tap="addAddress">
			<text>新增收货地址</text>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userdata: {
					openId: '',
					token: ''
				},
				addressList: [{
					province: '',
					city: '',
					county: '',
					detailAddress: '',
					recipientName: '',
					recipientPhone: '',
					userAddressId: '',
				}],

				swipe_options: [{
					text: '删除',
					style: {
						backgroundColor: '#e75e58'
					}
				}, ]
			}
		},
		methods: {
			delAddress(e, index) {
				console.log('点击了' + (e.position === 'left' ? '左侧' : '右侧') + e.content.text + '按钮' + 'index"' + index)
				let targetAddressId = this.addressList[index].userAddressId;
				console.log(targetAddressId, 'user_address_id');
				this.$myRequest({
					url: '/user/deleteAddress',
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						openId: this.userdata.openId,
						addressId: targetAddressId
					},
				}).then(res => {
					this.getAddress()
				}).catch(err => {
					console.log(err);
				})

			},
			swipeChange(e, index) {
				// console.log('当前状态：'+ e +'，下标：' + index)
			},
			/**
			 * 获取用户地址列表
			 */
			getAddressList() {
				const params = {
					url: '/user/getAddressList',
					method: 'GET',
					data: {},
					callBack: res => {
						this.addressList = res
					}
				}
				// http.request(params)
			},

			// 去编辑地址
			toEditAddress(addrId) {
				uni.navigateTo({
					url: '/pages/myaddress-edit/myaddress-edit?addrId=' + addrId
				})
			},

			/**
			 * 新建地址
			 */
			addAddress() {
				if (this.addressList.length === 10) {
					uni.showToast({
						title: "地址最多只能存10个",
						icon: 'none'
					})
				} else {
					uni.navigateTo({
						url: '/pages/myaddress-edit/myaddress-edit'
					})
				}
			},

			getAddress() {
				this.$myRequest({
					url: '/user/getAddressList',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						Authorization: this.userdata.token
					},
					data: {
						openId: this.userdata.openId,

					}
				}).then(res => {
					this.addressList = res.data.data
					console.log(res, "ssadasdsa")
				}).catch(err => {
					console.log(err)
				})

			},
			async getStorage() {
				const openId = uni.getStorageSync('openId')
				const token = uni.getStorageSync('token')
				this.userdata.openId = openId
				this.userdata.token = token
			},
			
		},

		onLoad() {
			this.getStorage()
			this.getAddress()
		}
	}
</script>

<style>
	@import "./myaddresslist.css";
</style>